package com.winning.sx.microframework.common.interceptor;

import com.winning.sx.microframework.dao.sysmanage.ModelManageDao;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.manage.ModelManageModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by xiaoY on 2016/12/9.
 */
public class MenuHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    public ModelManageDao modelManageDao;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        if (modelAndView == null)
            return;
        if (SecurityUtils.getSubject().isAuthenticated()) {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String systemid = ((LoginSession) session.getAttribute("loginSession")).getcSystemID();
            List<ModelManageModel> menus = null;
            menus = modelManageDao.queryModelsBySystemID(systemid);
            modelAndView.addObject("userMenus", menus);
        }
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
